﻿using System;
using System.IO;

namespace CreateReport
{
    class Operations
    {
        private int Days { get; set; }
                
        internal string Classification(string filename)
        {
            string result = "Error";
            if (filename.Contains("每日"))
            {
                result = "Daily";
            }
            if (filename.Contains("每周"))
            {
                result = "Weekly";
            }
            return result;
        }
                
        private int GetDaysInMonth(int year, int month)
        {
            Days = DateTime.DaysInMonth(year, month);
            return Days;
        }

        private string MakeDirectory(string year, string month, string option)
        {
            string folder = $"{ year }-{ month }";
            string subpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), option, folder);
            if (!Directory.Exists(subpath))
            {
                Directory.CreateDirectory(subpath);
            }
            return subpath;
        }

        private void CopyFiles(string srcfilename, string destfilename)
        {
            File.Copy(srcfilename, destfilename, true);
        }

        internal int CreateReport(string year, string province, string filecategory, string srcfilename)
        {
            int result = 0;
            if (filecategory == "Daily")
            {
                for (int i = 1; i <= 12; i++)
                {
                    string month;
                    if (i < 10)
                    {
                        month = $"0{ i }";
                    }
                    else
                    {
                        month = i.ToString();
                    }
                    Days = GetDaysInMonth(int.Parse(year), i);
                    string folder = MakeDirectory(year, month, "每日巡检报告");
                    for (int j = 1; j <= Days; j++)
                    {
                        string day;
                        if (j < 10)
                        {
                            day = $"0{ j }";
                        }
                        else
                        {
                            day = j.ToString();
                        }
                        string filename = $"{ province }_驻场工程师每日巡检表_{ year }_{ month }_{ day }.docx";
                        string destfilename = Path.Combine(folder, filename);
                        CopyFiles(srcfilename, destfilename);
                    }
                }
                result = 1;
            }
            else if (filecategory == "Weekly")
            {
                for (int i = 1; i <= 12; i++)
                {
                    string month;
                    if (i < 10)
                    {
                        month = $"0{ i }";
                    }
                    else
                    {
                        month = i.ToString();
                    }
                    Days = GetDaysInMonth(int.Parse(year), i);
                    string folder = MakeDirectory(year, month, "每周巡检报告");
                    for (int j = 1; j <= Days; j++)
                    {
                        string day;
                        if (j < 10)
                        {
                            day = $"0{ j }";
                        }
                        else
                        {
                            day = j.ToString();
                        }
                        int isMonday = DateTime.Parse($"{ year }-{ month }-{ day }").DayOfWeek - DayOfWeek.Sunday;
                        if (isMonday == 1)
                        {
                            string filename = $"{ province }_驻场工程师每周巡检表_{ year }_{ month }_{ day }.docx";
                            string destfilename = Path.Combine(folder, filename);
                            CopyFiles(srcfilename, destfilename);
                        }
                    }
                }
                result = 1;
            }
            return result;
        }
    }
}
